package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.MenuDao;
import com.itheima.health.dao.PermissionDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.pojo.Menu;
import com.itheima.health.pojo.Permission;
import com.itheima.health.service.MenuService;
import com.itheima.health.service.PermissionService;
import com.itheima.health.dao.MenuDao;
import com.itheima.health.dao.UserDao;
import com.itheima.health.pojo.Menu;
import com.itheima.health.pojo.User;
import com.itheima.health.service.MenuService;
import com.itheima.health.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 菜单服务
 */
// dubbo（整合声明式事务处理1：配置applicationContext-tx.xml对类代理，2：@Service(interfaceClass = CheckItemService.class)）
@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {

    @Autowired
    MenuDao menuDao;


    //新增菜单
    @Override
    public void add(Menu menu) {
        menuDao.add(menu);
    }

    //分页查询
    @Override
    public PageResult findPage(String queryString, Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        Page<Menu> page = menuDao.selectByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    //删除菜单
    @Override
    public void deleteById(Integer id) {
        //查询当前菜单是否和 角色 关联
        long count = menuDao.findCountByMenuId(id);
        if(count > 0){
            //当前检查项被引用，不能删除
            throw new RuntimeException("当前菜单被角色引用，不能删除");
        }
        menuDao.deleteById(id);

    }

    //主键ID查询菜单
    @Override
    public Menu findById(Integer id) {
        Menu menu = menuDao.findMenuById(id);
        return menu;
    }


    @Override
    public void edit(Menu menu) {
        menuDao.edit(menu);
    }

    @Override
    public List<Menu> findAll() {
        return menuDao.findAll();
    }

    // 菜单2-3
    @Override
    public List<Menu> findMenuZii(Integer id) {
        return menuDao.findMenuZii(id);
    }






}
